<?php
declare(strict_types=1);

namespace App\Exception;

use App\Constants\ErrorCode;
use Hyperf\Server\Exception\ServerException;

/**
 * Class BusinessException
 * User: caijw
 * DateTime: 2021/9/18 10:08
 * @package App\Exception
 */
class BusinessException extends ServerException
{
    private array $data = [];

    public function __construct(int $code = 0, array $param = [])
    {
        if (empty($param)) {
            $param = [""];//避免报错
        }
        parent::__construct(ErrorCode::getMessage($code, $param), $code);
    }

    public function setData($data): self
    {
        $this->data += $data;
        return $this;
    }

    public function getData(): array
    {
        return $this->data ?: [];
    }

    public function isCode(int $code): bool
    {
        return $this->code == $code;
    }
}
